package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Car extends Sprite
	{
		/**
		 * 极限速度
		 */
		private var _maxV:Number;
		private var maxVBack:Number;
		private var nitroMaxV:Number;
		/**
		 * 加速度
		 */
		private var _a:Number;
		private var nitroA:Number;
		private var backA:Number;
		/**
		 * 转向速度
		 */
		private var _angularV:Number;
		/**
		 * 漂移转向加速度
		 */
		private var _driftAngleAdd:Number;
		/**
		 * 最大漂移转向角度
		 */
		private var _maxDriftingAngle:Number;
		
		private var _currentV:Number=0;
		private var _currentAngularV:Number=0;
		private var _nitro:Boolean;
		private var _drifting:Boolean=false;
		private var angle:Number=0;
		/**
		 * 车头方向
		 */
		private var _headDirection:Number=0;
		private var driftAngle:Number=0;
		/**
		 * 阻力减速
		 */
		private var f:Number=0.05;
		
		private var vx:Number=0;
		private var vy:Number=0;
		
		private var _input:ICarInputSet;
		private var carMC:MovieClip;
		public function Car(maxV:Number,a:Number,angularV:Number,driftAngleAdd:Number,maxDriftingAngle:Number,input:ICarInputSet,mc:MovieClip)
		{
			_maxV=maxV;
			maxVBack=-_maxV/4;
			nitroMaxV=_maxV*1.1;
			
			_a=a;
			nitroA=_a*1.3;
			backA=2*_a;
			
			_angularV=angularV;
			
			_driftAngleAdd=driftAngleAdd;
			
			_maxDriftingAngle=maxDriftingAngle;
			
			carMC=mc;
			carMC.gotoAndStop(360);
			
			addChild(carMC);
			
			_input = input;
			onAdded();
		}
		private function onAdded():void{
			addEventListener(Event.ENTER_FRAME,onFrame);
		}
		public function onRemoved():void{
			removeEventListener(Event.ENTER_FRAME,onFrame);
			_input.removeEventListener();
		}
		private function onFrame(evt:Event):void{
			
			var ratioV:Number = _currentV/_maxV;
			//读取设备输入
			if(_input.isLeft){
				_currentAngularV = -_angularV*ratioV;
			}else if(_input.isRight){
				_currentAngularV = _angularV*ratioV;
			}else if(!_drifting){
				_currentAngularV=0;
			}
			if(_input.isUp){
				if(_nitro){
					_currentV+=nitroA;
				}else{
					_currentV += _a;
				}
			}else if(_input.isDown){
				_currentV += -(2*_a);
			}
			
			if(_drifting){
				//持续漂移
				var newDriftAng:Number;
				if(driftAngle > 0 && driftAngle < _maxDriftingAngle||driftAngle  >-_maxDriftingAngle && driftAngle < 0){
					if(_input.isRight) newDriftAng = driftAngle + _driftAngleAdd;
					else if(_input.isLeft) newDriftAng = driftAngle - _driftAngleAdd;
					
				}else if(driftAngle >= _maxDriftingAngle){
					newDriftAng = _maxDriftingAngle;
					if(_input.isLeft) newDriftAng = driftAngle - _driftAngleAdd;
				}else if(driftAngle <= -_maxDriftingAngle){
					newDriftAng = -_maxDriftingAngle;
					if(_input.isRight) newDriftAng = driftAngle + _driftAngleAdd;
				}
				//判断漂移结束
				if(newDriftAng*driftAngle>0){
					driftAngle = newDriftAng;
				}else{
					driftAngle = 0;
					_drifting = false;
					
					_currentAngularV = 0;
					angle = _headDirection;
				}
			}else{
				if(_input.isDrifting){
					//屏蔽低速漂移
					//						if(_currentV>10){
					if(_input.isLeft) driftAngle = -15;
					else if(_input.isRight) driftAngle = 15;
					_drifting = true;
					
					return;
					//						}
					
				}
			}
			
			//方向修正
			angle += _currentAngularV;
			_headDirection = angle+driftAngle;
			
			var tmpV:Number = _currentV;
			var num:Number = Math.sin(Math.abs(driftAngle)/180*Math.PI/4);
			if(_currentV>0 && _currentV>f) _currentV -= f+num;
			else if(_currentV<0 && _currentV<-f) _currentV +=  f+num;
			else{_currentV = 0}
			
			
			
			//速度极值修正
			if(_nitro){
				if(_currentV>nitroMaxV){
					_currentV=nitroMaxV;
				}else if(_currentV<maxVBack){
					_currentV=maxVBack;
				}
			}else{
				if(_currentV>_maxV){
					_currentV=_maxV;
				}else if(_currentV<maxVBack){
					_currentV=maxVBack;
				}
			}	
			
			

			var radian:Number = angle*Math.PI/180;
			vx=_currentV*Math.cos(radian);
			vy=_currentV*Math.sin(radian);
			x+=vx;
			y+=vy;
			

			
			var mcDirection:Number=_headDirection%360; 
			if(mcDirection<0){
				mcDirection=mcDirection+360;
			}
			mcDirection=Math.round(mcDirection);
			if(mcDirection==0){
				mcDirection=360;
			}
			carMC.gotoAndStop(mcDirection);
			
			
			trace(mcDirection);
			
		}
		
		
	}
}